package com.he.DesignPatterns.proxy.CGLibProxy;

import com.he.DesignPatterns.proxy.JdkProxy.SellTickets;
import com.he.DesignPatterns.proxy.JdkProxy.TrainStation;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @description: 作为访问对象与目标对象之间的中介
 * @Author: HCL
 * @Date: 2024/8/26 15:03
 */
public class ProxyFactory implements MethodInterceptor {


    public TrainStation getProxyObject() {
        //1 创建enhancer对象，类似与JDK中的Proxy类
        Enhancer enhancer = new Enhancer();
        //2 设置父类的字节码对象
        enhancer.setSuperclass(TrainStation.class);
        //3 设置回调函数
        enhancer.setCallback(this);
        //4 创建代理对象
        TrainStation trainStation = (TrainStation) enhancer.create();
        return trainStation;
    }

    private TrainStation station = new TrainStation();
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("代售点收取代理费_CGLIB代理");
        Object object = method.invoke(station, objects);
        return object;
    }
}
